clear;
clc;
close all;

DISPLAY = 0;
load('assignment1bases.mat')

bases1_1 = bases{1}(:,:,1);
bases1_2 = bases{1}(:,:,2);
bases1_3 = bases{1}(:,:,3);
bases1_4 = bases{1}(:,:,4);

bases2_1 = bases{2}(:,:,1);
bases2_2 = bases{2}(:,:,2);
bases2_3 = bases{2}(:,:,3);
bases2_4 = bases{2}(:,:,4);

bases3_1 = bases{3}(:,:,1);
bases3_2 = bases{3}(:,:,2);
bases3_3 = bases{3}(:,:,3);
bases3_4 = bases{3}(:,:,4);

e1_sum = 0;
e2_sum = 0;
e3_sum = 0;

mean = zeros(2,3);

% 构造矩阵，列向量为基向量
bases1 = [bases1_1(:), bases1_2(:), bases1_3(:), bases1_4(:)];
bases2 = [bases2_1(:), bases2_2(:), bases2_3(:), bases2_4(:)];
bases3 = [bases3_1(:), bases3_2(:), bases3_3(:), bases3_4(:)];

for j=1:2
    e1_sum = 0;
    e2_sum = 0;
    e3_sum = 0;
    for i=1:400
        img = stacks{j}(:,:,i);

        [img_hat1, error1] = PCA(img, bases1);
        [img_hat2, error2] = PCA(img, bases2);
        [img_hat3, error3] = PCA(img, bases3);

        e1_sum = e1_sum + error1;
        e2_sum = e2_sum + error2;
        e3_sum = e3_sum + error3;
        
        if DISPLAY == 1
            figure;
            subplot(4, 1, 1);
            imshow(uint8(img));
            title('Image');
    
            subplot(4, 1, 2);
            imshow(uint8(img_hat1));
            title('Image Base 1');
    
            subplot(4, 1, 3);
            imshow(uint8(img_hat2));
            title('Image Base 2');
    
            subplot(4, 1, 4);
            imshow(uint8(img_hat3));
            title('Image Base 3');
        end

    end

    mean(j, 1) = e1_sum / 400; % Calculate mean using Frobenius norm
    mean(j, 2) = e2_sum / 400; % Calculate mean using Frobenius norm
    mean(j, 3) = e3_sum / 400; % Calculate mean using Frobenius norm

end

mean
